package com.zhc.dejingapi.utils.fcmoroutingmain;

import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.QingAppfieldtablevaluesMapper;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.utils.qing.CommonAppFieldValueUpdateUtils;
import com.zhc.dejingapi.utils.qing.FCMoRoutingUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class FCPubInfo {
    public static FCMoRoutingUploadUtils fcInjectUploadUtils=null;
    public static CommonAppFieldValueUpdateUtils fcInjectUpdBackUtils=null;
    public static FCMoRoutingUploadUtils fcPressUploadUtils=null;
    public static CommonAppFieldValueUpdateUtils fcPressUpdBackUtils=null;

    public static FCMoRoutingUploadUtils fcWaterBoilEndUploadUtils=null;
    public static CommonAppFieldValueUpdateUtils LoadWaterBoildEndBackUtils=null;

    public static void LoadInjectUploadUtils(String strAppKey,
                                             QingflowApiInfo qqingflowApiInfo,
                                             QingAppfieldsMapper qingAppfieldsMapper,
                                             QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper,
                                             JdbcTemplate mysqlJdbcTemplate ) throws Exception{


        String strTableName = "fcmoroutingInject";//虚拟表名，不作回写，需要修改

        //region mapContrastFields&mapContrastDetailFields
        Map<String,String> mapContrastFields=new HashMap<>();

        mapContrastFields.put("生产任务单号","workTaskCode");//生产任务单号 workTaskCode
        mapContrastFields.put("产品编号","invCode");//产品编号 invCode
        mapContrastFields.put("产品名称","invName");//产品名称 invName
        mapContrastFields.put("规格型号","invStd");//规格型号 invStd
        mapContrastFields.put("计划产量","planQty");//计划产量 planQty
        mapContrastFields.put("工序名称","opName");//工序名称 opName
        mapContrastFields.put("批次号","proBatchNo");//批次号 proBatchNo
        mapContrastFields.put("设备编号","eqCode");//设备编号 eqCode
        mapContrastFields.put("模具编号","moldCode");//模具编号 moldCode
        mapContrastFields.put("班次","workShiftCode");//班次 workShiftCode
        mapContrastFields.put("生产人员","psnName");//生产人员 psnName
        mapContrastFields.put("员工工号","psnCode");//员工工号 psnCode
        mapContrastFields.put("报工开始时间","reportJobStartTime");//报工开始时间 reportJobStartTime
        mapContrastFields.put("报工结束时间","reportJobEndTime");//报工结束时间 reportJobEndTime
        mapContrastFields.put("报工方式","reportType");//报工方式 reportType
        //mapContrastFields.put("预计良品数","planQualfyQty");//预计良品数 planQualfyQty
        //mapContrastFields.put("报工后良品数","afterQualifyQty");//报工后良品数 afterQualifyQty
        //mapContrastFields.put("预计箱数","planNum");//预计箱数 planNum
        //mapContrastFields.put("剩余未清数量","leaveQty");//剩余未清数量 leaveQty
        //mapContrastFields.put("报工后剩余未清数","afterLeaveQty");//报工后剩余未清数 afterLeaveQty
        mapContrastFields.put("单箱数量/件","qtyPerCarton");//单箱数量/件 qtyPerCarton
        mapContrastFields.put("良品数","qualifyQty");//良品数 qualifyQty
        mapContrastFields.put("已完成数","finishedQty");//已完成数 finishedQty
        mapContrastFields.put("箱数","proNum");//箱数 proNum
        mapContrastFields.put("尾箱数量/件","modQty");//尾箱数量/件 modQty
        mapContrastFields.put("机台合模数","eqRunMoldQty");//机台合模数 eqRunMoldQty
        mapContrastFields.put("报工时间","reportJobTime");//报工时间 reportJobTime
        mapContrastFields.put("客户代号","cusCode");//客户代号 cusCode
        mapContrastFields.put("报工批次时间","workproTmBatch");//报工批次时间 workproTmBatch

        mapContrastFields.put("表格","qingFcmoroutingdetail");//报工批次时间 workproTmBatch


        Map<String,String> mapContrastDetailFields=new HashMap<>();

        mapContrastDetailFields.put("箱码","barcodeStr");//计划数  opQty
        mapContrastDetailFields.put("数量","iqty");//报工数配比  opRatio
        //endregion


        fcInjectUploadUtils =null;
        try{
            fcInjectUploadUtils = new FCMoRoutingUploadUtils(
                    qqingflowApiInfo,
                    mysqlJdbcTemplate,
                    qingAppfieldsMapper,
                    qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    mapContrastDetailFields,
                    strTableName);
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace().toString());
           throw e;
        }

        String strAppName="注塑任务单";
        String strSql="SELECT appKey from qing_appinfo where appName=?";
        String strSelAppKey=mysqlJdbcTemplate.queryForObject(strSql,String.class,strAppName);


        List<String> listUpdFields=new ArrayList<>();
        listUpdFields.add("已完成数量");
        listUpdFields.add("剩余未清数量");
        LoadInjectUpdBackUtils(qqingflowApiInfo,
                mysqlJdbcTemplate,
                qingAppfieldsMapper,
                qingAppfieldtablevaluesMapper,
                strSelAppKey,
                listUpdFields);

    }
    public static void LoadPressUploadUtils(String strAppKey,
                                            QingflowApiInfo qqingflowApiInfo,
                                            QingAppfieldsMapper qingAppfieldsMapper,
                                            QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper,
                                            JdbcTemplate mysqlJdbcTemplate ) throws Exception{


        String strTableName = "fcmoroutingpress";//虚拟表名，不作回写，需要修改



        //region mapContrastFields&mapContrastDetailFields
        Map<String,String> mapContrastFields=new HashMap<>();
        mapContrastFields.put("生产任务单号","workTaskCode");//生产任务单号 workTaskCode
        mapContrastFields.put("产品编号","invCode");//产品编号 invCode
        mapContrastFields.put("产品名称","invName");//产品名称 invName
        mapContrastFields.put("规格型号","invStd");//规格型号 invStd
        mapContrastFields.put("计划产量","planQty");//计划产量 planQty
        mapContrastFields.put("工序名称","opName");//工序名称 opName
        mapContrastFields.put("批次号","proBatchNo");//批次号 proBatchNo
        mapContrastFields.put("设备编号","eqCode");//设备编号 eqCode
        mapContrastFields.put("模具编号","moldCode");//模具编号 moldCode
        mapContrastFields.put("班次","workShiftCode");//班次 workShiftCode
        mapContrastFields.put("生产人员","psnName");//生产人员 psnName
        mapContrastFields.put("员工工号","psnCode");//员工工号 psnCode
        mapContrastFields.put("报工开始时间","reportJobStartTime");//报工开始时间 reportJobStartTime
        mapContrastFields.put("报工结束时间","reportJobEndTime");//报工结束时间 reportJobEndTime
        mapContrastFields.put("报工方式","reportType");//报工方式 reportType
        mapContrastFields.put("预计良品数","planQualfyQty");//预计良品数 planQualfyQty
        mapContrastFields.put("报工后良品数","afterQualifyQty");//报工后良品数 afterQualifyQty
        mapContrastFields.put("预计箱数","planNum");//预计箱数 planNum
        //mapContrastFields.put("剩余未清数量","leaveQty");//剩余未清数量 leaveQty
        //mapContrastFields.put("报工后剩余未清数","afterLeaveQty");//报工后剩余未清数 afterLeaveQty
        mapContrastFields.put("单箱数量/件","qtyPerCarton");//单箱数量/件 qtyPerCarton
        mapContrastFields.put("良品数","qualifyQty");//良品数 qualifyQty
        mapContrastFields.put("已完成数","finishedQty");//已完成数 finishedQty
        mapContrastFields.put("箱数","proNum");//箱数 proNum
        mapContrastFields.put("尾箱数量/件","modQty");//尾箱数量/件 modQty
        mapContrastFields.put("机台合模数","eqRunMoldQty");//机台合模数 eqRunMoldQty
        mapContrastFields.put("报工时间","reportJobTime");//报工时间 reportJobTime
        mapContrastFields.put("客户代号","cusCode");//客户代号 cusCode
        mapContrastFields.put("报工批次时间","workproTmBatch");//报工批次时间 workproTmBatch


        //报工后不良品数	8	afteScrapQty
        mapContrastFields.put("报工后不良品数","afteScrapQty");//报工后不良品数 afteScrapQty

        Map<String,String> mapContrastDetailFields=new HashMap<>();

        mapContrastDetailFields.put("箱码","barcodeStr");//计划数  opQty
        mapContrastDetailFields.put("数量","iqty");//报工数配比  opRatio
        //endregion
        fcPressUploadUtils=null;
        fcPressUploadUtils = new FCMoRoutingUploadUtils(qqingflowApiInfo,
                mysqlJdbcTemplate,
                qingAppfieldsMapper,
                qingAppfieldtablevaluesMapper,
                mapContrastFields,
                mapContrastDetailFields,
                strTableName);
        String strAppName="后道任务单";
        String strSql="SELECT appKey from qing_appinfo where appName=?";
        String strSelAppKey=mysqlJdbcTemplate.queryForObject(strSql,String.class,strAppName);

        List<String> listUpdFields=new ArrayList<>();
        listUpdFields.add("已完成数量");
        listUpdFields.add("剩余未清数量");
        LoadPressUpdBackUtils(qqingflowApiInfo,
                mysqlJdbcTemplate,
                qingAppfieldsMapper,
                qingAppfieldtablevaluesMapper,
                strSelAppKey,listUpdFields);

    }

    public static void LoadInjectUpdBackUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                                              QingAppfieldsMapper qingAppfieldsMapper1,
                                              QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                                              String strAppKey,
                                              List<String> listUpdFields)throws Exception{
        fcInjectUpdBackUtils=null;

        fcInjectUpdBackUtils=new CommonAppFieldValueUpdateUtils( qqingflowApiInfo1,  mysqlJdbcTemplate1,
                 qingAppfieldsMapper1,
                 qingAppfieldTableValuesMapper1,
                 strAppKey,
                listUpdFields);

    }

    public static void LoadPressUpdBackUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                                              QingAppfieldsMapper qingAppfieldsMapper1,
                                              QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                                              String strAppKey,
                                              List<String> listUpdFields)throws Exception{
        fcPressUpdBackUtils=null;

        fcPressUpdBackUtils=new CommonAppFieldValueUpdateUtils( qqingflowApiInfo1,  mysqlJdbcTemplate1,
                qingAppfieldsMapper1,
                qingAppfieldTableValuesMapper1,
                strAppKey,
                listUpdFields);

    }

    public static QingCommonBackResult UpdateTaskFinishQty(CommonAppFieldValueUpdateUtils utils, String strWorkTaskCode, JdbcTemplate mysqlJdbcTemplate){

        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        String strSql="SELECT sum(IFNULL(qualifyQty,0)) as qualifyQty\n" +
                "from qing_fcmoroutingmain \n" +
                "where IFNULL(qingApplyId,0)>0\n" +
                "and workTaskCode=?";
        String strQty=mysqlJdbcTemplate.queryForObject(strSql,String.class,strWorkTaskCode);



        strSql="UPDATE qing_sfcmoroutingshift set finishedQty=? ,leaveQty=planQty-finishedQty where qingRowCode=?";
        mysqlJdbcTemplate.update(strSql,new Object[]{strQty,strWorkTaskCode});



        strSql="SELECT qingApplyId from qing_sfcmoroutingshift where qingRowCode=?";
        String strQingApplyId=mysqlJdbcTemplate.queryForObject(strSql,String.class,strWorkTaskCode);

        strSql="SELECT leaveQty from qing_sfcmoroutingshift where qingRowCode=?";
        String leaveQty=mysqlJdbcTemplate.queryForObject(strSql,String.class,strWorkTaskCode);


        Map<String,String> mapFieldValue=new HashMap<>();
        mapFieldValue.put("已完成数量",strQty);
        mapFieldValue.put("剩余未清数量",leaveQty);
        //剩余未清数量
        commonBackResult=utils.UploadFieldValue(strQingApplyId,mapFieldValue);


        return commonBackResult;





    }

    public static void LoadWaterBoiledEndUpdBackUtils(String strAppKey,
                                                      QingflowApiInfo qqingflowApiInfo,
                                                      QingAppfieldsMapper qingAppfieldsMapper,
                                                      QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper,
                                                      JdbcTemplate mysqlJdbcTemplate ) throws Exception{


        String strTableName = "qing_waterboiltaskfinish";//虚拟表名，不作回写，需要修改

        //region mapContrastFields&mapContrastDetailFields
        Map<String,String> mapContrastFields=new HashMap<>();

        mapContrastFields.put("水煮任务单号","workTaskCode");//水煮任务单号 workTaskCode
        mapContrastFields.put("剩余未完成数","leaveQty");//剩余未完成数 leaveQty
        mapContrastFields.put("工单编号","workCode");//工单编号 workCode
        mapContrastFields.put("批次号","proBatchNo");//批次号 proBatchNo
        mapContrastFields.put("报工批次时间","proBatchTime");//报工批次时间 proBatchTime
        mapContrastFields.put("已完成数","finishQty");//已完成数 finishQty
        mapContrastFields.put("超时","timeOutTimeInStr");//超时 timeOutTimeInStr
        mapContrastFields.put("剩余时间","leaveTime");//剩余时间 leaveTime
        mapContrastFields.put("标准水煮时长","needBoilTime");//标准水煮时长 stdBoilTime
        mapContrastFields.put("水煮时长","boilTimeInStr");//水煮时长 boilTimeInStr
        mapContrastFields.put("水煮箱号","cartonCode");//水煮箱号 cartonCode
        mapContrastFields.put("实际结束时间","realEndBoilTime");//实际结束时间 realEndBoilTime
        mapContrastFields.put("水煮框","cartonName");//水煮框 cartonName
        mapContrastFields.put("计划数","planQty");//计划数 planQty
        mapContrastFields.put("预计结束时间","planEndTime");//预计结束时间 planEndBoilTime
        mapContrastFields.put("开始时间","startBoilTime");//开始时间 beginBoilTime
        mapContrastFields.put("报工后良品数","qualifyQty");//报工后良品数 qualifyQty
        mapContrastFields.put("产品名称","invName");//产品名称 invName
        mapContrastFields.put("产品编号","invCode");//产品编号 invCode
        //mapContrastFields.put("超时(按秒计时整数）","timeOutTimeInSecond");//员工工号 timeOutTimeInSecond
        mapContrastFields.put("班次","boilTimeInSecond");//班次 boilTimeInSecond
        mapContrastFields.put("员工工号","psnCode");//员工工号 psnCode
        mapContrastFields.put("生产人员","psnName");//生产人员 psnName
        mapContrastFields.put("水煮启动记录Id","boilStartRowNo");//水煮启动记录Id boilStartRowNo
        mapContrastFields.put("单框数量","eachCartonQty");//单框数量 eachCartonQty
        Map<String,String> mapContrastDetailFields=new HashMap<>();


        //endregion


        fcWaterBoilEndUploadUtils =null;
        try{
            fcWaterBoilEndUploadUtils = new FCMoRoutingUploadUtils(
                    qqingflowApiInfo,
                    mysqlJdbcTemplate,
                    qingAppfieldsMapper,
                    qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    mapContrastDetailFields,
                    strTableName);
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            throw e;
        }

        String strAppName="水煮启动报工";
        String strSql="SELECT appKey from qing_appinfo where appName=?";
        String strSelAppKey=mysqlJdbcTemplate.queryForObject(strSql,String.class,strAppName);


        List<String> listUpdFields=new ArrayList<>();
        listUpdFields.add("已完成数");
        listUpdFields.add("剩余未清数");

        LoadWaterBoildEndBackUtils(qqingflowApiInfo,
                mysqlJdbcTemplate,
                qingAppfieldsMapper,
                qingAppfieldtablevaluesMapper,
                strSelAppKey,
                listUpdFields);

    }

    public static void LoadWaterBoildEndBackUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                                             QingAppfieldsMapper qingAppfieldsMapper1,
                                             QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                                             String strAppKey,
                                             List<String> listUpdFields)throws Exception{
        LoadWaterBoildEndBackUtils=null;

        LoadWaterBoildEndBackUtils=new CommonAppFieldValueUpdateUtils( qqingflowApiInfo1,  mysqlJdbcTemplate1,
                qingAppfieldsMapper1,
                qingAppfieldTableValuesMapper1,
                strAppKey,
                listUpdFields);

    }
    public static QingCommonBackResult UpdateWaterBoilStartQty(CommonAppFieldValueUpdateUtils utils, String strBoilStartRowNo, JdbcTemplate mysqlJdbcTemplate){

        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        String strSql="SELECT sum(IFNULL(qualifyQty,0)) as qualifyQty\n" +
                "from qing_waterboiltaskfinish \n" +
                "where IFNULL(qingApplyId,0)>0\n" +
                "and boilStartRowNo=?";
        String strQty=mysqlJdbcTemplate.queryForObject(strSql,String.class,strBoilStartRowNo);


        strSql="UPDATE qing_waterboiltaskstart set finishedQty=? ,leaveQty=planQty-finishedQty where qingRowCode=?";
        mysqlJdbcTemplate.update(strSql,new Object[]{strQty,strBoilStartRowNo});


        strSql="SELECT qingApplyId from qing_waterboiltaskstart where qingRowCode=?";
        String strQingApplyId=mysqlJdbcTemplate.queryForObject(strSql,String.class,strBoilStartRowNo);

        strSql="SELECT leaveQty from qing_waterboiltaskstart where qingRowCode=?";
        String leaveQty=mysqlJdbcTemplate.queryForObject(strSql,String.class,strBoilStartRowNo);


        Map<String,String> mapFieldValue=new HashMap<>();
        mapFieldValue.put("已完成数",strQty);
        mapFieldValue.put("剩余未清数",leaveQty);
        //剩余未清数量
        commonBackResult=utils.UploadFieldValue(strQingApplyId,mapFieldValue);

        return commonBackResult;

    }

}
